home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12253 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  40 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!dirkv
  3. From: dirkv@netcom.com (Dirk Vermeersch)
  4. Subject: Re: What is wrong with this code?
  5. Message-ID: <dirkvDoHs79.MJr@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. References: <4il0pn$fp@news1.io.org>
  9. Date: Tue, 19 Mar 1996 01:59:32 GMT
  10. Sender: dirkv@netcom.netcom.com
  11.  
  12. Ivan (ticica@io.org) wrote:
  13.  
  14. :   Please, can someone tell me what's wrong with this program? It's driving 
  15. : me nuts.  When I compile it with Borland C++ 3.1 I get null pointer 
  16. : assignment. With Watcom C++ 9.5 the last line (cout << a * 3) prints some 
  17. : junk. Thanks in advance.
  18. :     
  19. :                             Ivan
  20. : -------------------------------------------------------------------------
  21.  
  22.  
  23. Hi Ivan,
  24.  
  25. The problem is in the operator * function :
  26. a new vector is created on the stack and than returned. This would 
  27. be OK if the vector had a copy constructor (vector(const &vector)), but
  28. here the default copy constructor is used. The default copy constructor
  29. just copies the members ( shallow copy ==> 'data' and 'name' point to the
  30. same memory as for the retvec on the stack ).
  31. Upon returning from the function the vector's destructor is called. This
  32. will delete the name and the data members, but these pointers are still
  33. being used in the object returned by the function.
  34.  
  35. Dirk
  36.  
  37.  
  38. :     
  39.  
  40.